package com.example.util;

import com.example.pojo.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HtmlParseUtil {
    public static void main(String[] args) throws IOException {
        List<Content> goodsList = getGoodsList("金瓶梅");
        goodsList.forEach(System.out::println);
    }

    public  static   List<Content> getGoodsList(String keyWords) throws IOException {
        //        https://search.jd.com/Search?keyword=java&enc=utf-8&wq=java&pvid=6c8805955059418da57f8153387a1766
        String url = "https://search.jd.com/Search?keyword="+keyWords;
        Document document =  Jsoup.parse(new URL(url),30000);
        Element element = document.getElementById("J_goodsList");
        Elements lis = element.getElementsByTag("li");
        ArrayList<Content> goods = new ArrayList<>();

        for (Element li : lis) {
            Elements imgs = li.getElementsByTag("img");
            Elements prices = li.getElementsByClass("p-price");
            Elements names = li.getElementsByClass("p-name");
            Content content = new Content();


            for (Element img : imgs) {

                    content.setImg(img.attr("data-lazy-img"));
            }

            for (Element price : prices) {

                content.setPrice(price.text());
            }

            for (Element name : names) {

                content.setTitle(name.text());
            }
            goods.add(content);
        }
        return goods;
    }
}
